package com.sz.mes.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sz.mes.entity.CastMaterial;
import com.sz.mes.entity.Technique;
import com.sz.mes.mapper.CastMaterialMapper;
import com.sz.mes.mapper.TechniqueMapper;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.diboot.core.vo.*;
import com.diboot.iam.annotation.OperationCons;
import com.diboot.iam.annotation.BindPermission;
import com.diboot.iam.annotation.Log;
import com.sz.mes.entity.Output;
import com.sz.mes.dto.OutputDTO;
import com.sz.mes.vo.OutputListVO;
import com.sz.mes.vo.OutputDetailVO;
import com.sz.mes.service.OutputService;
import com.sz.mes.controller.BaseCustomCrudRestController;
import lombok.extern.slf4j.Slf4j;
import javax.validation.Valid;
import java.util.List;

/**
 * 产出 相关Controller
 *
 * @author hem
 * @version 1.0
 * @date 2024-01-15
 * Copyright © szhs
 */
@RestController
@RequestMapping("/output")
@BindPermission(name = "产出")
@Slf4j
public class OutputController extends BaseCustomCrudRestController<Output> {

    @Autowired
    private OutputService outputService;

    @Autowired
    private CastMaterialMapper castMaterialMapper;

    @Autowired
    private TechniqueMapper techniqueMapper;

    /**
     * 查询ViewObject的分页数据
     * <p>
     * url请求参数示例: /list?field=abc&pageIndex=1&orderBy=abc:DESC
     * </p>
     *
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_LIST)
    // @BindPermission(name = OperationCons.LABEL_LIST, code = OperationCons.CODE_READ)
    @GetMapping("/list")
    public JsonResult getViewObjectListMapping(OutputDTO queryDto, Pagination pagination) throws Exception {
        return super.getViewObjectList(queryDto, pagination, OutputListVO.class);
    }

    /**
     * 根据资源id查询ViewObject
     *
     * @param id ID
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_DETAIL)
    // @BindPermission(name = OperationCons.LABEL_DETAIL, code = OperationCons.CODE_READ)
    @GetMapping("/{id}")
    public JsonResult getViewObjectMapping(@PathVariable("id") Long id) throws Exception {
        return super.getViewObject(id, OutputDetailVO.class);
    }

    /**
     * 创建资源对象
     *
     * @param entity
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_CREATE)
    // @BindPermission(name = OperationCons.LABEL_CREATE, code = OperationCons.CODE_WRITE)
    @PostMapping("/")
    public JsonResult createEntityMapping(@Valid @RequestBody Output entity) throws Exception {
        QueryWrapper<CastMaterial> cqw = new QueryWrapper<>();
        cqw.eq("tech_id", entity.getTechId());
//        cqw.eq("is_used", 0);
        List<CastMaterial> castMaterialList = castMaterialMapper.selectList(cqw);
        for (CastMaterial castMaterial : castMaterialList) {
//            castMaterial.setIsUsed(1);
            castMaterialMapper.updateById(castMaterial);
        }

        Technique technique = techniqueMapper.selectById(entity.getTechId());
        QueryWrapper<Technique> tqw = new QueryWrapper<>();
        tqw.eq("tech_name",technique.getTechName());
        tqw.eq("task_num",technique.getTaskNum());
        List<Technique> techniqueList = techniqueMapper.selectList(tqw);
        if( techniqueList.size() != 1 ){
            for (Technique technique1: techniqueList){
                technique1.setOutput((long) (technique1.getOutput()+entity.getGoodQuantity()+entity.getBadQuantity()));
                techniqueMapper.updateById(technique1);
            }
        }else {
            technique.setOutput((long) (entity.getGoodQuantity()+entity.getBadQuantity()));
            techniqueMapper.updateById(technique);
        }
        return super.createEntity(entity);
    }

    /**
     * 根据ID更新资源对象
     *
     * @param entity
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_UPDATE)
    // @BindPermission(name = OperationCons.LABEL_UPDATE, code = OperationCons.CODE_WRITE)
    @PutMapping("/{id}")
    public JsonResult updateEntityMapping(@PathVariable("id") Long id, @Valid @RequestBody Output entity) throws Exception {
        return super.updateEntity(id, entity);
    }

    /**
     * 根据id删除资源对象
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_DELETE)
    // @BindPermission(name = OperationCons.LABEL_DELETE, code = OperationCons.CODE_WRITE)
    @DeleteMapping("/{id}")
    public JsonResult deleteEntityMapping(@PathVariable("id") Long id) throws Exception {
        return super.deleteEntity(id);
    }

    /**
     * 查询投料数据
     *
     * @param
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_DETAIL)
    // @BindPermission(name = OperationCons.LABEL_DETAIL, code = OperationCons.CODE_READ)
    @GetMapping("/findDetail")
    public JsonResult findDetail(String taskNum) throws Exception {
        return outputService.findDetail(taskNum);
    }
}
